package com.seventh.parkingrent.controller;

import com.seventh.parkingrent.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;

@RestController
@Slf4j
public class MapController {

    private final String KEY="nx5he56j8XZDIhXpih2eSaG8OdcHcpqc";

    @Autowired
    RestTemplate restTemplate;
    @GetMapping("/getip")
    public Result getUserIp(HttpServletRequest request) throws URISyntaxException {
        log.info("根据ip获取地址");
        String ip = request.getRemoteAddr();
        log.info("得到ip:{}",ip);
        String url="https://api.map.baidu.com/location/ip?ak="+KEY+"&ip="+ip+"&coor=bd09ll";
        //向百度地图发送请求获得地址信息
        ResponseEntity<Map> forEntity = restTemplate.getForEntity(new URI(url), Map.class);
        Map body=forEntity.getBody();
        return new Result();
    }
}
